addpath([pwd,"/lib"]);
addpath([pwd,"/src"]);

load("rec.dat");
load("tstl.dat");

stats=[rec, tstl];

scores=[];
iterations = 3;
startTime = cputime;
confidenceLevel=0.925;

#a quick test over the test set
allCm = getConfusionMatrices(rec, tstl);
[allAns allConf] = bayesianMeta(rec, allCm, 0.000001);
allAns(allConf<confidenceLevel)=10;
allScore=[sum(allAns==tstl)/size(rec,1), sum(allAns==10)/size(tstl,1)];
printf("BAYESIAN META: results on whole dataset: correct: %f, discarded: %f, errors: %f \n", allScore(1,1), allScore(1,2), 1-sum(allScore));

for(trial=1:iterations)
		
	[trainSet testSet] = splitSet([rec, tstl], 0.5);
	
	confMatrices = getConfusionMatrices(trainSet(:,1:end-1), trainSet(:,end));
	[testAnswers confidence] = bayesianMeta(testSet(:,1:end-1), confMatrices); 
	
	testAnswers(confidence<confidenceLevel)=10;
	scores=[scores; sum(testAnswers==testSet(:,6))/size(testSet,1), sum(testAnswers==10)/size(testSet,1)];
	if(mod(trial,25)==0)
		printf("ITERATION: %d, CPU-TIME ELAPSED: %f minutes \n",trial,(cputime-startTime)/60);
		printf("Mean-> score : %f, discarded: %f \n",mean(scores(:,1)),mean(scores(:,2)));
		fflush(stdout);
	end;
end;

printf("Average over %d iterations -> score: %f, discarded: %f \n", iterations, mean(scores(:,1)), mean(scores(:,2)));
